﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;
namespace WindowsGame4
{
    public class Asteroid : Damageable
    {
        public int level;
        public const int maxLevel=5, minLevel=1;
        public Asteroid(int level=0)
            : base()
        {
            if (level == 0)
                level = (int)ParticleEmitter.GetRandom((float)minLevel, (float)maxLevel);
            texture = GetTexture();
            this.level = (int)MathHelper.Clamp(level, minLevel, maxLevel);
            SetScale();
        }
        public static Texture2D GetTexture()
        {
            int id = GameManager.random.Next(8);
            return GameManager.asteroidTextures[id];
        }
        public void SetScale()
        {
            Vector2 scale = new Vector2(2 * (float)level / (float)maxLevel);
            this.scale = scale;
        }
        public override void Update(float time)
        {
            base.Update(time);
            Vector2 dir = pos - GameManager.player.pos;
            float distance = dir.Length();
            if (distance > GameManager.asteroidDeviance)
            {
                bExplodeOnDeath = false;
                bDead = true;
            }
        }
        public override void OnDeath()
        {
            if (!bExplodeOnDeath)
                return;
            if(level>minLevel)
                for (int i = 0; i < 3; i++)
                {
                    Asteroid a = new Asteroid(level - 1);
                    Vector2 dir=GameObject.GetDir(120 * i);
                    float radius = a.GetRadius()+1;
                    a.pos = pos + dir*radius;
                    a.vel = moveSpeed * dir;
                    a.rotSpeed = rotSpeed;
                    GameManager.AddObject(a);
                }
            GameManager.explosion.Play();
            Mineral mineral = new Mineral(10 * level);
            mineral.texture = GetTexture();
            mineral.lifeTime = 10;
            mineral.scale = new Vector2((float)level / (float)(maxLevel * 3));
            mineral.color = new Vector4(0, 0, 1, 1);
            mineral.rotSpeed = ParticleEmitter.GetRandom(-180, 180);
            mineral.pos = pos;
            GameManager.AddObject(mineral);
            ParticleEmitter debris = new DebrisEmitter();
            debris.pos = pos;
            debris.lifeTime = 1;
            debris.target = debris;
            debris.moveSpeed = -300;
            debris.maxDistance = GetRadius();
            debris.SetEmissionRate(0);
            debris.nEmits = 20;
            GameManager.AddObject(debris);
            base.OnDeath();
        }
    }
}
